﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.warehouse
{
    public partial class goodsIn : BaseAdmin
    {
        readonly DALGoodsType theGoodsType = new DALGoodsType();
        readonly DALGoods theGoods = new DALGoods();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(ERule.WarehouseManager);
                this.BindGoodsTree();
                this.BindCardTree();

            }
        }


        void BindGoodsTree()
        {
            DataTable tbGoodsType = theGoodsType.GetList("typeName = '产品专区'").Tables[0];
            DataTable tbGoods = theGoods.GetList("isnull(visible,0) =1 and typeId in(select typeId from tbGoodsType where typeDepth=2) ").Tables[0];

            DataTable tbGoodsTypeSub = theGoodsType.GetListForHavGoods().Tables[0];
            DataTable tbGoodsTypeSubEnd = theGoodsType.GetListForHavGoods().Tables[0];
            DataView dvType;
            DataView dvSubType;
            DataView dvGoods;

           if(tbGoodsType != null && tbGoodsType.Rows.Count > 0)
            {
            
                tbGoodsTypeSub.DefaultView.RowFilter = string.Format("parentId = {0}", tbGoodsType.Rows[0]["typeId"].ToString());
                dvType = tbGoodsTypeSub.DefaultView;
                for (int i = 0; i < dvType.Count; i++)
                {
                    BindTreeNode(dvType[i]["typeId"].ToString(), dvType[i]["typeName"].ToString(), dvType[i]["typeId"].ToString(), dvType[i]["typeName"].ToString(), 1);
                }


                for (int i = 0; i < dvType.Count; i++)
                {
                    tbGoodsTypeSubEnd.DefaultView.RowFilter = string.Format("parentId = {0}", dvType[i]["typeId"].ToString());
                    dvSubType = tbGoodsTypeSubEnd.DefaultView;
                    for (int n = 0; n < dvSubType.Count; n++)
                    {
                        BindTreeNode(dvSubType[n]["parentId"].ToString(), dvSubType[n]["parentName"].ToString(), dvSubType[n]["typeId"].ToString(), dvSubType[n]["typeName"].ToString(), 2);

                        tbGoods.DefaultView.RowFilter = string.Format("typeId = {0}", dvSubType[n]["typeId"].ToString());
                        dvGoods = tbGoods.DefaultView;
                        for (int m = 0; m < dvGoods.Count; m++)
                        {
                            BindTreeNode(dvSubType[n]["typeId"].ToString(), dvSubType[n]["typeName"].ToString(), dvGoods[m]["goodsId"].ToString(), dvGoods[m]["goodsName"].ToString(), 3);
                        }
                    }
                }


            }
  


        }

        void BindTreeNode(string parentId, string parentName, string subId, string subName, int level)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = parentId;
                node.SingleClickExpand = true;
                node.Text = parentName;
                node.Target = "main";
                if (level == 1)
                {
                    nodeIndex = FindNodeById(parentId);

                      //  node.NavigateUrl = "openCardList.aspx?id=" + node.NodeID;
                    this.treeGoods.Nodes.Add(node);

                }
            }



            if (this.treeGoods.FindNode(parentId) != null && level == 2)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = subId;
                node.SingleClickExpand = true;
                node.Target = "main";
                //  node.NavigateUrl = "openCardList.aspx?id=" + node.NodeID;
                node.Text = subName;
                this.treeGoods.FindNode(parentId).Nodes.Add(node);
            }
            else if (this.treeGoods.FindNode(parentId) != null && level == 3)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = "gid_" + subId;
                node.SingleClickExpand = true;
                node.Target = "main";
                node.NavigateUrl = "warehouseGoods.aspx?gid=" + subId;
                node.Text = subName;
                this.treeGoods.FindNode(parentId).Nodes.Add(node);
            }

        }

        int FindNodeById(string sNodeId)
        {
            for (int i = 0; i < this.treeGoods.Nodes.Count; i++)
            {
                if (this.treeGoods.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
            }
            return -1;

        }



        //================================
        void BindCardTree()
        {
            DataTable tbGoodsType = theGoodsType.GetList("typeName = '卡券专区'").Tables[0];
            DataTable tbGoods = theGoods.GetList("isnull(visible,0) =1 and typeId in(select typeId from tbGoodsType where typeDepth=2) ").Tables[0];

            DataTable tbGoodsTypeSub = theGoodsType.GetListForHavGoods().Tables[0];
            DataTable tbGoodsTypeSubEnd = theGoodsType.GetListForHavGoods().Tables[0];
            DataView dvType;
            DataView dvSubType;
            DataView dvGoods;

            if (tbGoodsType != null && tbGoodsType.Rows.Count > 0)
            {

                tbGoodsTypeSub.DefaultView.RowFilter = string.Format("parentId = {0}", tbGoodsType.Rows[0]["typeId"].ToString());
                dvType = tbGoodsTypeSub.DefaultView;
                for (int i = 0; i < dvType.Count; i++)
                {
                    BindTreeCardNode(dvType[i]["typeId"].ToString(), dvType[i]["typeName"].ToString(), dvType[i]["typeId"].ToString(), dvType[i]["typeName"].ToString(), 1);
                }


                for (int i = 0; i < dvType.Count; i++)
                {
                    tbGoodsTypeSubEnd.DefaultView.RowFilter = string.Format("parentId = {0}", dvType[i]["typeId"].ToString());
                    dvSubType = tbGoodsTypeSubEnd.DefaultView;
                    for (int n = 0; n < dvSubType.Count; n++)
                    {
                        BindTreeCardNode(dvSubType[n]["parentId"].ToString(), dvSubType[n]["parentName"].ToString(), dvSubType[n]["typeId"].ToString(), dvSubType[n]["typeName"].ToString(), 2);

                        tbGoods.DefaultView.RowFilter = string.Format("typeId = {0}", dvSubType[n]["typeId"].ToString());
                        dvGoods = tbGoods.DefaultView;
                        for (int m = 0; m < dvGoods.Count; m++)
                        {
                            BindTreeCardNode(dvSubType[n]["typeId"].ToString(), dvSubType[n]["typeName"].ToString(), dvGoods[m]["goodsId"].ToString(), dvGoods[m]["goodsName"].ToString(), 3);
                        }
                    }
                }


            }



        }

        void BindTreeCardNode(string parentId, string parentName, string subId, string subName, int level)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = parentId;
                node.SingleClickExpand = true;
                node.Text = parentName;
                node.Target = "main";
                if (level == 1)
                {
                    nodeIndex = FindNodeById(parentId);

                    //  node.NavigateUrl = "openCardList.aspx?id=" + node.NodeID;
                    this.treeCard.Nodes.Add(node);

                }
            }



            if (this.treeCard.FindNode(parentId) != null && level == 2)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = subId;
                node.SingleClickExpand = true;
                node.Target = "main";
                //  node.NavigateUrl = "openCardList.aspx?id=" + node.NodeID;
                node.Text = subName;
                this.treeCard.FindNode(parentId).Nodes.Add(node);
            }
            else if (this.treeCard.FindNode(parentId) != null && level == 3)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = "gid_" + subId;
                node.SingleClickExpand = true;
                node.Target = "main";
                node.NavigateUrl = "warehouseGoods.aspx?gid=" + subId;
                node.Text = subName;
                this.treeCard.FindNode(parentId).Nodes.Add(node);
            }

        }

        int FindCardNodeById(string sNodeId)
        {
            for (int i = 0; i < this.treeCard.Nodes.Count; i++)
            {
                if (this.treeCard.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
            }
            return -1;

        }

    }
}